#!/usr/bin/env python
# -*- coding: utf-8 -*-
from tool.gongju import *
from tool.zhibiao import ma_z, ema_z, atr_z
import tool.MyTT as myttzb


RXZQ = {}  # 按合约储存日线周期k线和指标数据
XIAOSHIZQ = {}  # 按合约储存小时周期k线和指标数据


def 日线周期指标计算_v1(cl):
    """
    日线周期指标计算，用于给下面策略周期调用
    """
    # 数据提取区
    # Open = cl.klines[0].open.values  # 开盘价np列表
    # High = cl.klines[0].high.values   # 最高价np列表
    # Low = cl.klines[0].low.values  # 最低价np列表
    Close = cl.klines[0].close.values  # 收盘价np列表，Close[-1]这样为最新价

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量， 也是控制策略的开关
    n1 = cl.jiaoyishezhi[5][1]  # ma1周期
    n2 = cl.jiaoyishezhi[5][2]  # ma2周期
    交易合约 = cl.jiaoyishezhi[3][0]
    # 指标计算区
    ma1 = myttzb.MA(Close, n1)
    ma2 = myttzb.MA(Close, n2)

    # 数据合并与储存
    df = cl.klines[0]
    df['ma1'] = ma1
    df['ma2'] = ma2
    # 向下移动一位，防止偷价
    df['ma1'] = (df['ma1'].shift(1)).fillna(method='bfill')
    df['ma2'] = (df['ma2'].shift(1)).fillna(method='bfill')
    df = df.set_index(df['datetime'])
    RXZQ[交易合约] = df
    # print(df)
    return 'w', 'w', 'w', 'w'  # 此处输出的指标会在k线图中和监控界面中显示


def 小时周期指标计算_v1(cl):
    """
    小时周期指标计算，用于给下面策略周期调用
    """
    # 数据提取区
    # Open = cl.klines[0].open.values  # 开盘价np列表
    # High = cl.klines[0].high.values   # 最高价np列表
    # Low = cl.klines[0].low.values  # 最低价np列表
    Close = cl.klines[0].close.values  # 收盘价np列表，Close[-1]这样为最新价

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量， 也是控制策略的开关
    n1 = cl.jiaoyishezhi[5][1]  # ma1周期
    n2 = cl.jiaoyishezhi[5][2]  # ma2周期
    交易合约 = cl.jiaoyishezhi[3][0]
    # 指标计算区
    ma1 = myttzb.MA(Close, n1)
    ma2 = myttzb.MA(Close, n2)

    # 数据合并与储存
    df = cl.klines[0]
    df['ma1'] = ma1
    df['ma2'] = ma2
    # 向下移动一位，防止偷价
    df['ma1'] = (df['ma1'].shift(1)).fillna(method='bfill')
    df['ma2'] = (df['ma2'].shift(1)).fillna(method='bfill')

    df = df.set_index(df['datetime'])
    XIAOSHIZQ[交易合约] = df

    return 'w', 'w', 'w', 'w'  # 此处输出的指标会在k线图中和监控界面中显示


def 唐琪安通道跨周期过滤_v1(cl):
    """
    此策略为唐琪安通道突破演示策略，用日线周期和小时线周期过滤
    """
    # 数据提取区
    Open = cl.klines[0].open.values  # 开盘价np列表
    High = cl.klines[0].high.values   # 最高价np列表
    Low = cl.klines[0].low.values  # 最低价np列表
    Close = cl.klines[0].close.values  # 收盘价np列表，Close[-1]这样为最新价
    Datetime = cl.klines[0].datetime.values  # 日期时间np列表，为时间戳
    Date = cl.klines[0].date.values  # 日期
    canshu = [cl.jiaoyiqidong, cl.zhanghu, cl.jiaoyishezhi, cl.openingdata, Datetime]  # 传递给交易函数用于交易
    chicang = cl.openingdata['kaicangzhuangtai'] *cl.openingdata['kaicangshuliang'] # 持仓状态和持仓数量
    kaicangjia = cl.openingdata['kaicangjia']  # 开仓价格

    # 参数设置区
    ss = cl.jiaoyishezhi[5][0]  # 开仓数量
    n1 = cl.jiaoyishezhi[5][1]+1  # 高点周期
    n2 = cl.jiaoyishezhi[5][2]+1  # 低点周期
    交易合约 = cl.jiaoyishezhi[3][0]

    # 全局变量的初始化，为空的时候执行
    if '日周期均线' not in cl.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl.sjb['日周期均线'] = 0
        cl.sjb['时周期均线'] = 0

    # 指标计算区
    gd = max(High[-n1:-1])
    dd = min(Low[-n2:-1])
    try:
        cl.sjb['日周期均线'] = RXZQ[交易合约].loc[Date[-1]]['ma1']
    except:
        pass

    try:
        cl.sjb['时周期均线'] = XIAOSHIZQ[交易合约].loc[Datetime[-1]]['ma1']
    except:
        pass

    # 交易逻辑执行区
    if chicang <= 0 and High[-1] >= gd and Close[-2] > cl.sjb['日周期均线'] and Close[-2] > cl.sjb['时周期均线']:
        Buy(ss, max(Open[-1], gd), canshu)
    else:
        if chicang >= 0 and Low[-1] <= dd and Close[-2] < cl.sjb['日周期均线'] and Close[-2] < cl.sjb['时周期均线']:
            SellShort(ss, min(Open[-1], dd), canshu)

    return gd, dd, cl.sjb['日周期均线'], cl.sjb['时周期均线']
